package com.ningxia.water.controller.auth;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.ningxia.water.entity.SysUserTable;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.SysUserService;
import com.ningxia.water.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"权限控制-用户管理"})
public class SysUserController extends ApiController {

    @Autowired
    SysUserService userService;


    /**
     * 查询所有数据集
     * @return
     */
    @GetMapping("/getinfos")
    @ApiOperation(value = "获取所有用户")
    public Result getList() {

        List<SysUserTable> list =  userService.list();
        return  ResultResponse.getSuccessResult(list);
    }

    /**
     * 添加用户、用户自行注册。
     * @param userVo
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result register(@RequestBody(required = false) SysUserVo userVo) {
       try {
           return  ResultResponse.getSuccessResult(userService.register(userVo));
       }catch (Exception e){
           return ResultResponse.getFailResult(e.getMessage());
       }
    }

    /**
     * 修改数据
     *
     * @param sysUserVo 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateuser")
    @ApiOperation(value = "修改用户")
    public Result update(@RequestBody SysUserVo sysUserVo) {
        //对用户密码进行加密
        SysUserTable po = this.userService.voToPo(sysUserVo);
        boolean isUpdate = userService.updateById(po);
        if(isUpdate){
            return ResultResponse.getSuccessResult("更新成功");
        }else{
            return ResultResponse.getFailResult("更新失败");
        }

    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteuser")
    @ApiOperation(value = "删除用户")
    public Result delete(@RequestParam("idList") List<String> idList) {
        boolean isDelete = userService.removeByIds(idList);
        if (isDelete){
            return ResultResponse.getSuccessResult("删除成功");
        }else {
            return  ResultResponse.getFailResult("删除失败");
        }

    }
}
